package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;


import pms.common.value.UserSessionValue;

public class UserDaoImpl implements UserDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	public UserSessionValue authUser(String username, String password) {
		String sql = "SELECT * FROM TBL_USER where USER_NAME=? AND USER_PASSWORD=?";
		final UserSessionValue l_userSessionValue = new UserSessionValue();
		
		jdbcTemplate.query(sql, new Object[]{username, password}, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				l_userSessionValue.setUserId(rs.getInt("USER_ID"));
				l_userSessionValue.setUserName(rs.getString("USER_NAME"));
				l_userSessionValue.setUserCompany(rs.getString("USER_COMPANY"));
				l_userSessionValue.setUserDispName(rs.getString("USER_DISP_NAME"));
				l_userSessionValue.setUserGroupId(rs.getInt("USER_GROUP_ID"));
				l_userSessionValue.setUserRights(rs.getString("USER_RIGHTS"));
				l_userSessionValue.setUserProjectId(rs.getInt("USER_PROJECT_ID"));
			}

			
		});
		if(l_userSessionValue.getUserName()!= null)
			return l_userSessionValue;
		else
			return null;
	}


	public List<UserSessionValue> getUserList() {
		String sql = "SELECT * FROM TBL_USER order by USER_ID";
		final List<UserSessionValue> l_result = new ArrayList<UserSessionValue>();
		jdbcTemplate.query(sql, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				UserSessionValue l_userSessionValue = new UserSessionValue();
				l_userSessionValue.setUserId(rs.getInt("USER_ID"));
				l_userSessionValue.setUserName(rs.getString("USER_NAME"));
				l_userSessionValue.setUserCompany(rs.getString("USER_COMPANY"));
				l_userSessionValue.setUserDispName(rs.getString("USER_DISP_NAME"));
				l_userSessionValue.setUserGroupId(rs.getInt("USER_GROUP_ID"));
				l_userSessionValue.setUserRights(rs.getString("USER_RIGHTS"));
				l_userSessionValue.setUserProjectId(rs.getInt("USER_PROJECT_ID"));
				l_result.add(l_userSessionValue);
			}
		});
		return l_result;
	}

}
